// pages/user/user.js
let OPENID = ''
let name=''
let image = ''
let sex = ''
let information = ''
let Address = ''
Page({
  data:{
    list:[],
    Order:[],
    num:[],
    locationName:null
  },
  onLoad() {
      this.getopenid()
      this.getadd()
  },
  onShow(){
    this.getUserProfile()
  },
  //获取个人信息
   getUserProfile() {
      wx.cloud.database().collection('user-information')
      .where({
        _openid:OPENID
      })
      .get()
      .then(res=>{   
        this.setData({
          list:res.data,
        })
        console.log('用户信息查询成功',res)
      })
      .catch(err=>{
        console.log('条件查询失败')
      })
  },
  getadd(){
    wx.cloud.callFunction({
      name:'getAddress',
    })
    .then(res=>{
      console.log('res:',res)
      if(res.result.data.length == 0){
        wx.showToast({
          title: '请先选择您的位置',
          icon:'none',
         })
        }else{
     Address = res.result.data[0].address
     console.log('云函数获取地址成功：',Address)
     this.setData({
       locationName: Address?Address: ""
     })
    }
    })
    .catch(err=>{
      console.log('云函数获取地址失败：',err)
    })
   },
  getOrder(){
    let that =this
    wx.cloud.database().collection('All-Orders')
    .where({
      openid:OPENID
    })
    .get()
    .then(res=>{  
      this.setData({
        Order:res.data,
      })
      console.log('Order：',that.data.Order)
      console.log('list：',that.data.list)
      console.log('用户订单信息查询成功',res)
    })
    .catch(err=>{
      console.log('用户订单信息查询失败',err)
    })
  },
  //获取openid
   getopenid(){
      wx.cloud.callFunction({
        name:'getOPENID'
      })
      .then(res=>{
        console.log('成功获取openID',res)
        this.setData({
          OPENID:res.result.openid
        })
        OPENID = res.result.openid
        this.getUserProfile()
        this.getOrder()
      })
      .catch(res=>{
        console.log('失败',res)
      })
    },
   goMygoods(e){
    wx.navigateTo({
      url: '/pages/Mygoods/Mygoods?openid='+OPENID,
    })
    console.log('成功跳转到我的发布页面，携带的OPENID为：',OPENID)
  },
  goUserOrder(e){
    let ty =e.currentTarget.dataset.type
    console.log('e:',e.currentTarget.dataset.type)
    wx.navigateTo({
      url: '/pages/UserOrder/UserOrder?ty='+ty+'&openid='+OPENID,
    })
  },
  goScanCode(){
    wx.navigateTo({
      url: '/pages/ScanCode/ScanCode'
    })
  },
    goChange(e){
      console.log('点击了商品详情页',e.currentTarget.dataset.id)
      wx.navigateTo({
        url: '/pages/change/change?id='+e.currentTarget.dataset.id,
      })
    },
    goTOOLS(){
      wx.navigateTo({
        url: '/pages/Other-tools/Other-tools'
      })
    },
    goVoucher(){
      wx.navigateTo({
        url: '/pages/User-Voucher/User-Voucher'
      })
    },
    goShoppingCart(){
      wx.navigateTo({
        url: '/pages/ShoppingCart/ShoppingCart?openid='+OPENID
      })
    },
    goMyAddress(){
      wx.navigateTo({
        url: '/pages/User-MyAddress/User-MyAddress?openid='+OPENID
      })
    },
    changeUser(){
      wx.navigateTo({
        url: '/pages/UserInformation/UserInformation?OpenID='+OPENID
      })
    },
    goClause(){
      console.log('跳转到规则中心')
      wx.navigateTo({
        url: '/pages/clause/clause'
      })
    },
    goguanyu(){
      console.log('跳转到关于')
      wx.navigateTo({
        url: '/pages/User-guanyu/User-guanyu'
      })
    },
    gohezuo(){
      console.log('跳转到合作')
      wx.navigateTo({
        url: '/pages/User-hezuo/User-hezuo'
      })
    },
    gokefu(){
      console.log('跳转到客服')
      wx.navigateTo({
        url: '/pages/User-kefu/User-kefu'
      })
    }
})

